package org.autotestcase.action;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class GenericBean
{
  protected final Log logger;

  public GenericBean()
  {
    this.logger = LogFactory.getLog(super.getClass()); }

  protected FacesContext getFacesContext() {
    return FacesContext.getCurrentInstance();
  }

  protected HttpServletResponse getResponse() {
    return ((HttpServletResponse)getFacesContext().getExternalContext().getResponse());
  }

  protected HttpServletRequest getRequest() {
    return ((HttpServletRequest)getFacesContext().getExternalContext().getRequest());
  }

  protected ServletContext getServletContext() {
    return ((ServletContext)getFacesContext().getExternalContext().getContext());
  }

  public String getParameter(String name) {
    return getRequest().getParameter(name);
  }

  protected HttpSession getSession() {
    return getRequest().getSession();
  }

  public ResourceBundle getBundle() {
    return ResourceBundle.getBundle(getBundleName(), getRequest().getLocale());
  }

  public String getBundleName() {
    return getFacesContext().getApplication().getMessageBundle();
  }

  public String getMessage(String key) {
    try {
      return getBundle().getString(key);
    } catch (MissingResourceException mre) {
      this.logger.warn("Chave [" + key + "] nao encontrada."); }
    return "";
  }

  public String getMessage(String key, String[] arg)
  {
    MessageFormat form = new MessageFormat(getMessage(key));
    return form.format(arg);
  }

  public void reset()
  {
  }
}